<template>
  <div class="app-container search-list">
    <table-pagination
        ref="el-search-table"
        :adaptive="fixOption"
        :columns="columns"
        :fetch="getData"
        :form-options="formOptions"
        :page-sizes="[20, 50, 100]"
        height="300"
        list-field="records"
        method="get"
        page-index-key="page"
        page-size-key="pageSize"
        total-field="totalElements"
        type="remote">
      <template #button>
        <router-link :to="{name: 'design-list'}">
          <el-button plain icon="el-icon-back" size="small" :round="true">
            返回
          </el-button>
        </router-link>
        <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="handleAdd">创建组合</el-button>
      </template>
      <el-table-column slot="append" label="操作" width="250" fixed="right">
        <template slot-scope="scope">
          <el-button plain size="mini" type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button plain size="mini" type="danger" @click="deleteDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </table-pagination>
  </div>
</template>

<script>
import TablePagination from '_c/Tables/table-pagination';
import {getTiedUpList, delTiedUp} from '@/api/app-market';
import {mapGetters} from 'vuex';

export default {
    name: 'tied-up',
    components: {TablePagination},
    data () {
        return {
            fixOption: {fixedHeader: true},
            pageParams: {
                page: 1,
                pageSize: 20
            },
            formOptions: {
                showResetBtn: false,
                showSearchBtn: false,
                inline: false,
                labelWidth: 120,
                submitBtnText: '查询',
                size: 'small',
                resetBtnCallback: () => {
                    this.$refs['el-search-table'].fetchHandler(this.pageParams, false);
                },
                forms: [
                ]
            },
            columns: [
                {label: '捆绑内部名称', prop: 'name'},
                {label: '捆绑显示名称', prop: 'displayName'},
                {label: '捆绑商品数量', prop: 'bundleSpuNum'},
                {label: '应用商品数', prop: 'applySpuNum'}
            ]
        };
    },
    methods: {
        getData (params) {
            params.siteId = this.siteId;
            return new Promise((resolve, reject) => {
                getTiedUpList(params).then((res) => {
                    resolve(res);
                });
            });
        },
        handleAdd () {
            this.$router.push({name: 'tied-up-add'});
        },
        // 优惠码编辑
        handleEdit (id) {
            this.$router.push({name: 'tied-up-edit', params: {id}});
        },
        deleteDataById (id) {
            this.$confirm('此操作将删除数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delTiedUp(id).then(res => {
                    if (res.code === '200') {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.$refs['el-search-table'].fetchHandler(this.pageParams, false);
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消操作'
                });
            });
        }
    },
    computed: {
        ...mapGetters(['siteList']),
        siteId () {
            return this.$store.state.user.siteId;
        },
        siteInfo () {
            let info = this.siteList.find(res => res.id === Number(this.siteId));
            return info;
        }
    }
};
</script>

<style lang="scss" scoped>
.search-list {
  h4 {
    border-bottom: 1px solid #E8E8E8;
    margin: 0;
    margin-bottom: 10px;
    padding-bottom: 10px;
  }
}
</style>
